/**
 * Project Name:Load-PlatForm-Server
 * File Name:XDateUtils.java
 * Package Name:com.jjt.homexpress.loadplatform.server.utils
 * Date:2015年11月19日上午11:02:54
 * Copyright (c) 2015, dengxb All Rights Reserved.
 *
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 */
/**
 * @Title: XDateUtils.java
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 * @version 1.0
 */

package com.shuoyue.ycgk.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

/**
 * ClassName:XDateUtils <br/>
 * Date:     2015年11月19日 上午11:02:54 <br/>
 * @author dengxb
 * @version
 * @since JDK 1.6
 * @see
 */

/**
 * @Title: XDateUtils
 * @Description:
 * @Company: homexpress
 * @author dengxb
 * @date 2015年11月19日 上午11:02:54
 */
public class XDateUtils {
    private static XDateUtils instance;
    private Date date;
    private Calendar calendar;

    /**
     * @Title: getInstance
     * @Description: 单利模式
     * @return
     */
    public static XDateUtils getInstance() {
        if (instance == null) {
            instance = new XDateUtils();
            instance.date = new Date();
            instance.calendar = Calendar.getInstance();
        }
        return instance;
    }

    /**
     * @Title: format
     * @Description: 格式化时间
     * @param timestamp
     * @param pattern
     * @return
     */
    public static String format(Long timestamp, String pattern) {
        if (timestamp == null || timestamp == 0) {
            return "";
        }
        String result = "";
        try {
            getInstance().date.setTime(timestamp);
            SimpleDateFormat format = new SimpleDateFormat(pattern,
                    Locale.getDefault());
            result = format.format(instance.date);
        } catch (Exception e) {
            result = "";
        }
        return result;
    }

    /**
     * @Title: format
     * @Description: 格式化成倒计时格式, 保留一位毫秒
     * @param time
     * @return
     */
    public static String format(long time, int mill) {
        if (mill < 0) {
            mill = 0;
        }
        if (time < 0) {
            return "";
        }
        // 时
        long house = time / 3600;
        time = time % 3600;
        // 分
        long minute = time / 60;
        // 秒
        long sec = time % 60;

        String h = house < 10 ? "0" + house : "" + house;
        String m = minute < 10 ? "0" + minute : "" + minute;
        String s = sec < 10 ? "0" + sec : "" + sec;
        return h + ":" + m + ":" + s + "." + (mill / 100);
    }

    /**
     * @Title: format
     * @Description: 格式化成倒计时格式, 保留一位毫秒
     * @param time
     * @return
     */
    public static String formatHHmmss(long time) {
        long second = time / 1000;
        if (second < 0) {
            return "";
        }
        // 时
        long house = second / 3600;
        second = second % 3600;
        // 分
        long minute = second / 60;
        // 秒
        long sec = second % 60;

        String h = house < 10 ? "0" + house : "" + house;
        String m = minute < 10 ? "0" + minute : "" + minute;
        String s = sec < 10 ? "0" + sec : "" + sec;
        return h + ":" + m + ":" + s;
    }

    /**
     * @Title: format
     * @Description: 格式化成倒计时格式, 保留一位毫秒
     * @param time
     * @return
     */
    public static String formatDDHHmm(long time) {
        long second = time / 1000;
        if (second < 0) {
            return "";
        }
        long day = second / 3600 / 24;//天数
        long lastExDay = second - day * (3600 * 24);//除天后剩余秒数
        long hours = lastExDay / 3600;//小时数
        long lastExHours = lastExDay - hours * 3600;//除小时候剩余秒数
        long min = lastExHours / 60;//分
        long lastSecond = lastExHours % 60;//秒数
        String d = day < 10 ? "0" + day : "" + day;
        String h = hours < 10 ? "0" + hours : "" + hours;
        String m = min < 10 ? "0" + min : "" + min;
        String s = lastSecond < 10 ? "0" + lastSecond : "" + lastSecond;
        return (day == 0 ? "" : (d + "天")) + h + "时" + m + "分" + s + "秒";
    }

    /**
     * 时间转化为时间格式（返回格式自定义）
     * @param timeStamp
     * @return
     */
    public static String timeStampToStr(long timeStamp, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String date = sdf.format(timeStamp);
        return date;
    }

    /**
     * 日期变量转成对应的星期字符串
     *
     * @param date
     * @return
     */

    public static final int WEEKDAYS = 7;
    // 星期字符数组
    public static String[] WEEK = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    /**
     * 把字符串转为日期
     */
    public static Date cover(String strDate, String format) throws Exception {
        DateFormat df = new SimpleDateFormat(format);
        return df.parse(strDate);
    }

    /**
     * 把字符串转为日期
     */
    public static Date ConverToDate(String strDate) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.parse(strDate);
    }

    /**
     * 把字符串转为日期
     */
    public static Date ConverToDateTime(String strDate) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.parse(strDate);
    }

    /**
     * 餐饮美食-点餐、外卖、订位判断当前时间是否在营业时间内
     * 判断某一时间是否在一个区间内
     *
     * @param sourceTime 时间区间,半闭合,如[10:00-20:00)
     * @param curTime    需要判断的时间 如10:00
     * @return boolean
     * @throws IllegalArgumentException
     */
    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();

            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            } else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }

    }

    /**
     * 返回指定日期的季度
     *
     * @param date
     * @return
     */
    public static int getQuarterOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) / 3 + 1;
    }


    // 获得本周一0点时间
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     * 得到某年某周的最后一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        week = week - 1;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DATE, 1);
        Calendar cal = (Calendar) calendar.clone();
        cal.add(Calendar.DATE, week * 7);
        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 取得当前日期所在周的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK,
                calendar.getFirstDayOfWeek()); // Sunday
        return calendar.getTime();
    }

    /**
     * 取得当前日期所在周的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK,
                calendar.getFirstDayOfWeek() + 6); // Saturday
        return calendar.getTime();
    }

    /**
     * 返回指定年月的月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month, 1);
        return calendar.getTime();
    }

    /**
     * 返回指定年月的月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * 返回指定年季的季的第一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 1 - 1;
        } else if (quarter == 2) {
            month = 4 - 1;
        } else if (quarter == 3) {
            month = 7 - 1;
        } else if (quarter == 4) {
            month = 10 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getFirstDayOfMonth(year, month);
    }

    /**
     * 返回指定年季的季的最后一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 3 - 1;
        } else if (quarter == 2) {
            month = 6 - 1;
        } else if (quarter == 3) {
            month = 9 - 1;
        } else if (quarter == 4) {
            month = 12 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getLastDayOfMonth(year, month);
    }

    /**
     * 返回指定年季的上一季的最后一天
     *
     * @param year
     * @param quarter
     * @return
     */
    public static Date getLastDayOfLastQuarter(Integer year, Integer quarter) {
        Calendar calendar = Calendar.getInstance();
        Integer month = new Integer(0);
        if (quarter == 1) {
            month = 12 - 1;
        } else if (quarter == 2) {
            month = 3 - 1;
        } else if (quarter == 3) {
            month = 6 - 1;
        } else if (quarter == 4) {
            month = 9 - 1;
        } else {
            month = calendar.get(Calendar.MONTH);
        }
        return getLastDayOfMonth(year, month);
    }

    //获取当天的开始时间
    public static Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    //获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    //获取本周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }

    //获取上周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }

    //获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    //获取今年是哪一年
    public static Integer getNowYear() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return Integer.valueOf(gc.get(1));
    }

    //获取本月是哪一月
    public static int getNowMonth() {
        Date date = new Date();
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        gc.setTime(date);
        return gc.get(2) + 1;
    }

    //获取某年某月按天切片日期集合（某个月间隔多少天的日期集合）
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static List getTimeList(int beginYear, int beginMonth, int k) {
        List list = new ArrayList();
        Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
        int max = begincal.getActualMaximum(Calendar.DATE);
        for (int i = 1; i < max; i = i + k) {
            list.add(begincal.getTime());
            begincal.add(Calendar.DATE, k);
        }
        begincal = new GregorianCalendar(beginYear, beginMonth, max);
        list.add(begincal.getTime());
        return list;
    }


    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String MDHM_DATE_PATTERN = "MM-dd HH:mm";
    public static final String YMDHM_DATE_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String HM_DATE_PATTERN = "HH:mm";

    /**
     * 时间戳转时间
     *
     * @param timestamp
     * @return
     */
    public static String timestamp2String(long timestamp, String pattern) {
        String dateString = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        dateString = sdf.format(new Date(timestamp));
        return dateString;
    }


}
